package com.sfx.算法专题.链表.合并链表;

import java.util.PriorityQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2023-07-30
 * Time: 14:15
 */
public class Code_2_mergeKLists {
    static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
            next = null;
        }
    }
    static class Solution {
        public ListNode mergeKLists(ListNode[] lists) {
            PriorityQueue<Integer> pq = new PriorityQueue<Integer>((o1, o2) -> o1-o2);
            for(ListNode head : lists) {
                while(head!=null) {
                    pq.add(head.val);
                    head = head.next;
                }
            }
            ListNode dummy = new ListNode(-1);
            ListNode tmp = dummy;
            while(!pq.isEmpty()) {
                tmp.next = new ListNode(pq.poll());
                tmp = tmp.next;
            }
            return dummy.next;
        }
    }
}
